package com.luo.service.user.impl;

import com.luo.dao.UserDao;
import com.luo.pojo.User;
import com.luo.service.user.UserService;
import com.luo.utils.PasswordEncoderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public boolean hasUser(String username) {
        return this.userDao.hasUser(username).size() > 0;
    }

    @Override
    public boolean hasPhone(String phoneNumber) {
        return this.userDao.hasPhone(phoneNumber).size() > 0;
    }

    @Override
    public int userReg(User user) {
        try{
            this.userDao.regUser(user);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    //获取用户信息
    public User getUserByNumber(String phoneNumber) {
        return this.userDao.hasPhone(phoneNumber).get(0);
    }
    @Override
    public boolean isPwd(String phoneNumber,String password) {
       boolean flag = PasswordEncoderUtil.checkPassword(password,this.userDao.hasPhone(phoneNumber).get(0).getPassword());
        return flag;
    }

    @Override
    public User getUserByName(String username) {
        return this.userDao.hasUser(username).get(0);
    }
}
